<template>
	<view>
		<!-- 微信小程序内 微信登录 -->
		<button open-type="getUserInfo" @getuserinfo="wxGetUserInfo" style="background-color: #000000;color: #ffffff;position: fixed;left: 0;bottom: 350upx;width: 82%;height: 78upx;line-height: 78upx;margin: 0 9%;">小程序微信登录</button>
		
		
		<!-- app内微信登录 -->
		<view class="weixin">
			<image src="../../../static/function/wx_load.png" style="width: 124upx;height: 74upx;margin-left: -48upx;" @click="wx_load"></image>
			<text class="lg_1">app微信登录</text>
			<text class="lg_2">登录及代表您已阅读并同意</text>
			<text class="lg_3">《服务协议》《隐私协议》《管理制度》</text>
		</view>
	</view>
</template>

<script>
	export default {
		methods: {
			wxGetUserInfo: (res)=> {
				// #ifdef MP-WEIXIN
				   uni.setStorage({
				   	key: 'timestamp',
				   	data: res.timeStamp // 基本无用,要求就存
				   })
				   uni.setStorage({
				   	key: 'token',
				   	data: '1be42a581e1592a85bdfe6f8174e391f' // 基本无用,要求就存
				   })
				   let result = res
				   uni.login({
				    provider: "weixin",
				    success:function(loginRes) {
				   		uni.request({ 
				   			url: '', // 信息传给后台,然后后台返回数据再缓存
				   			method: 'POST',
				   			data: {
				   				code: loginRes.code,
				   				encryptedData: result.detail.encryptedData,
				   				iv: result.detail.iv,
				   				rawData: result.detail.rawData,
				   				signature: result.detail.signature,
				   				// pid:谁分享带谁的用户id,一般二级分销用的
				   			},
				   			header:{
				   				'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
				   			},
				   			success: (detail) => {
				   				// let d = new Date().getTime() + 8600000; // 设置session过期时间,一般项目不用,需要设置登录过期模块时打开,
				   				// uni.setStorage({
				   				// 	key: "expire_date",
				   				// 	data:	d
				   				// })
				   				// uni.setStorage({
				   				// 	key:	"sessionid",
				   				// 	data:	detail.data.session
				   				// })
				   				let details = detail.data.data
				   				// 获取用户信息再存储
				   				if (details.status == 2) {
				   					uni.setStorage({
				   						key: 'user_name',
				   						data: details.user_name
				   					})
				   					uni.setStorage({
				   						key: 'phone_number',
				   						data: details.phone_number
				   					})
				   					uni.setStorage({
				   						key: 'province_id',
				   						data: details.province_id
				   					})
				   					uni.setStorage({
				   						key: 'city_id',
				   						data: details.city_id
				   					})
				   					uni.setStorage({
				   						key: 'district_id',
				   						data: details.district_id
				   					})
				   					uni.setStorage({
				   						key: 'level_id',
				   						data: details.level_id
				   					})
				   					uni.setStorage({
				   						key: 'user_id',
				   						data: details.id,
				   						success: function () {
				   							uni.showToast({
				   								title: '登录成功',
				   								icon: 'none'
				   							})
				   							setTimeout(function() {
				   								uni.reLaunch({
				   									url: '../index/index'
				   								})
				   							}, 1700);
				   						}
				   					})
				   				}
				   			}
				   		});
				   	}
				   })
				   if(!res.detail.iv) {
				    uni.showModal({
				   	title:"温馨提示",
				   	content:"为了更好的购买体验，需要获取您的信息",
				   	success:function(resModal) {
				   	 if(res.confirm) {
				   		uni.openSetting({}) // 不支持 app h5
				   	 }
				   	}
				    });
				    return false
				   }
				// #endif
			},
			
			wx_load() {
				// #ifndef APP-PLUS
					uni.showToast({
						title: '在app上才能生效',
						icon: 'none'
					})
				// #endif
				// #ifdef APP-PLUS
					// 记得在manifest.json中配置打开相关项
					uni.login({
						success: (res) => {
							 uni.getUserInfo({
								success: (info) => {
									// 与服务器交互将数据提交到服务端数据库
									uni.request({
										url: '',
										method: 'POST',
										header: {'content-type' : "application/x-www-form-urlencoded"},
										data: {
											 openid: info.userInfo.openId,
											 nickname: info.userInfo.nickName,
											 avatar: info.userInfo.avatarUrl,
											 province: info.userInfo.province,
											 city: info.userInfo.city,
										},
										success: res => {
											if(res.statusCode == 200) {
												// 存后台返回数据, 跳转页面
												uni.setStorage({
													key: 'openid',
													data: res.data.openid,
													success: (res) => {},
													fail: () => {}
												})
												setTimeout(()=>{
													uni.reLaunch({
														url: '../member/member'
													});
												},500)
												uni.showToast({
													title: '登陆成功',
													mask: false,
													duration: 1500
												});
											}else{
												uni.showToast({title:'微信登陆失败'});
											}
										},
										fail: () => {
											console.log('登陆失败')
										},
										complete: () => {}
									});
	
								},
								fail: () => {
									uni.showToast({title:"微信登录授权失败"});
								}
							})
						},
						fail: () => {
							uni.showToast({title:"微信登录授权失败"});
						}
					})
				// #endif
			},
		}
	}
</script>

<style lang="scss">
	.weixin {
		position: fixed;left: 0;bottom: 100upx;
		// position: relative;
		margin-top: 530upx;
		text-align: center;
		z-index: 999;
		display: flex;
		flex-wrap: wrap;
		image {padding-left: 50%;margin-left: -40upx;}
		text {width: 100%;}
		.lg_1 {font-size: 22upx; margin-bottom: 20upx; color: #000;}
		.lg_2 {font-size: 20upx; color: #888;}
		.lg_3 {font-size: 20upx; color: #3c56ff;}
	}
</style>
